package com.msh.artascope.web.common.bean;

import com.msh.artascope.interfaces.IAccountAuth;
import com.msh.artascope.interfaces.IAccountMenu;
import com.msh.artascope.web.common.config.ConfigLoginConfiguration;
import com.msh.artascope.web.common.model.DefaultAccountMenu;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

public class ConfigLoginAuth implements IAccountAuth {
    private final Long sysId;
    private final List<DefaultAccountMenu> accountMenus;
    private final Set<String> authCodes;
    private final Set<String> roleCodes;

    public ConfigLoginAuth(ConfigLoginConfiguration configuration) {
        sysId = configuration.getSysId();
        accountMenus = new LinkedList<>();
        List<ConfigLoginConfiguration.Auth> auths = configuration.getAuths();
        if(!CollectionUtils.isEmpty(auths)){
            auths.forEach(a->{
                accountMenus.add(
                        new DefaultAccountMenu()
                        .setId(a.getId())
                        .setName(a.getName())
                        .setPath(a.getPath())
                        .setType(a.getType())
                );
            });
        }
        String auth = configuration.getAuthCodes();
        authCodes = new HashSet<>();
        if(StringUtils.hasText(auth)){
            authCodes.addAll(Arrays.asList(auth.split(",")));
        }
        String role = configuration.getRoleCodes();
        roleCodes = new HashSet<>();
        if(StringUtils.hasText(role)){
            roleCodes.addAll(Arrays.asList(role.split(",")));
        }
    }

    @Override
    public List<? extends IAccountMenu> getAccountMenu() {
        return accountMenus;
    }

    @Override
    public Set<String> getAuthCodes() {
        return authCodes;
    }

    @Override
    public Set<String> getRoleCodes() {
        return roleCodes;
    }

    @Override
    public Long getSysId() {
        return sysId;
    }
}
